/**
 * Copyright (c) 2011, www.quartzsource.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.quartzsource

import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArraySeq

package object core {

  type Binary = scala.collection.mutable.ArraySeq[Byte]
  object Binary {

    def apply(): Binary = new scala.collection.mutable.ArraySeq[Byte](0)

    def apply(data: Array[Byte]): Binary = {
      val result = new ArraySeq[Byte](data.length)
      data.copyToArray(result.array.asInstanceOf[Array[Any]], 0)
      result
    }

    def apply(data: String): Binary = {
      stringToBytes(data)
    }

    def read(input: java.io.InputStream): Binary = {
      require(input != null)
      val result = new ArrayBuffer[Byte](0)
      val buffer = new Array[Byte](1024)
      Stream.continually(
        input.read(buffer)).takeWhile(_ != -1).foreach(result ++= buffer.take(_))
      Binary(result.toArray)
    }
  }

  //not yet implicit
  def bytesToString(bytes: Binary): String = {
    new String(bytes.toArray, "UTF-8")
  }

  def stringToBytes(str: String): Binary = {
    Binary(str.getBytes("UTF-8"))
  }

  def translate(formatter: String, args: Any*): String = {
    val javaStyle = formatter.replaceAll("%r", "%s")
    translateToLocal(javaStyle.format(args: _*))
  }

  private def translateToLocal(english: String): String = {
    //TODO translation is not yet implemented. '*.po' files can be used.
    english
  }

  // Scala does not support unsigned Long
  type IndexFormatNG = (Long, Int, Int, Int, Int, Int, Int, NodeId)
  val IndexFormatNG_SIZE = 64

  // (state, mode, size, time)
  type DirStateTuple = (DirState.FileState, Int, Int, Int)

}